
#if defined(WIN32) || defined(WIN64)
#pragma unmanaged
#endif

#include <Liberty3D/Engine/GameObject.h>
#include <Liberty3D/Engine/Component.h>
#include <Liberty3D/Engine/Context.h>

#include <Liberty3D/Engine/ISerializer.h>

using namespace Liberty::Engine;

GameObject::GameObject(const std::shared_ptr<Context>& context, const std::string& name) : Object(), _context(context), _name(name), _active(true), _layer(0), _tag(0) {
}

void GameObject::setParent(const std::shared_ptr<GameObject>& gameObject) {
    _parent = gameObject;
}

std::string GameObject::name() const {
    return _name;
}

void GameObject::setName(const std::string& name) {
    _name = name;
}

void GameObject::setActive(bool a) {
    _active = a;
}

void GameObject::setLayer(int l) {
    _layer = l;
}

void GameObject::setTag(int t) {
    _tag = t;
}

std::shared_ptr<Component> GameObject::addComponent(const std::string& name) {
    std::shared_ptr<Component> c(ComponentFactory::create(name.c_str()));

    if (c) {
        context()->addComponent(shared_from_this(), c);
    }

    return c;
}

void GameObject::serializeObject(ISerializer* s) {
    s->serialize("parent",        _parent);
    s->serialize("name",          _name);
    s->serialize("localPosition", _localPosition);
    s->serialize("localRotation", _localRotation);
    s->serialize("localScale",    _localScale);
    s->serialize("active",        _active);
    s->serialize("layer",         _layer);
    s->serialize("tag",           _tag);
}

const char* GameObject::serializeClassName() const {
    return "Liberty::Engine::GameObject";
}

void GameObject::serializeEnd(ISerializer*) {
}
